# !/usr/bin/env python
# -*-coding:utf-8 -*-
"""
# File       : permission_view.py
# Time       ：2023/7/9 16:42
# Author     ：Y-aong
# version    ：python 3.7
# Description
    权限视图：
    permission views
"""
from flask_restx import fields

from apis.system_oauth import system_oauth_ns
from apis.system_oauth.models import SystemPermission
from apis.system_oauth.schema.permission_schema import SystemPermissionSchema
from public.api_utils.api_decorator import handle_api_error
from public.base_response import MarshalModel
from public.base_view import BaseView, BaseDetailsView

permission = {
    'name': fields.String(description='权限名称'),
    'menu': fields.Boolean(description='是否菜单'),
    'method': fields.String(description='请求方法'),
    'path': fields.String(description='请求路径正则'),
    'pid': fields.Integer(description='权限父ID'),
    'desc': fields.String(description='部门描述'),
}

permission_marshal = MarshalModel('permission', system_oauth_ns)


class PermissionDetailView(BaseDetailsView):
    url = '/permission/detail'

    def __init__(self, *args, **kwargs):
        super(PermissionDetailView, self).__init__(*args, **kwargs)
        self.table_orm = SystemPermission
        self.table_schema = SystemPermissionSchema

    @handle_api_error()
    @system_oauth_ns.doc(description="获取权限详情")
    @system_oauth_ns.expect(permission_marshal.detail_request())
    @system_oauth_ns.marshal_with(permission_marshal.detail_response(permission))
    def get(self):
        """获取权限详情"""
        return super(PermissionDetailView, self).get()


class PermissionView(BaseView):
    url = '/permission'

    def __init__(self, *args, **kwargs):
        super(PermissionView, self).__init__(*args, **kwargs)
        self.table_orm = SystemPermission
        self.table_schema = SystemPermissionSchema

    @handle_api_error()
    @system_oauth_ns.doc(description='获取权限列表')
    @system_oauth_ns.expect(permission_marshal.list_request(permission))
    @system_oauth_ns.marshal_with(permission_marshal.list_response(permission))
    def get(self):
        """获取权限列表"""
        return super(PermissionView, self).get()

    @handle_api_error()
    @system_oauth_ns.doc(description='创建权限')
    @system_oauth_ns.expect(permission_marshal.create_request(permission))
    @system_oauth_ns.marshal_with(permission_marshal.create_response())
    def post(self):
        """创建权限"""
        return super(PermissionView, self).post()

    @handle_api_error()
    @system_oauth_ns.doc(description='修改权限')
    @system_oauth_ns.expect(permission_marshal.update_request(permission))
    @system_oauth_ns.marshal_with(permission_marshal.update_response())
    def put(self):
        """修改权限"""
        return super(PermissionView, self).put()

    @handle_api_error()
    @system_oauth_ns.doc(description='删除权限')
    @system_oauth_ns.expect(permission_marshal.delete_request())
    @system_oauth_ns.marshal_with(permission_marshal.delete_response())
    def delete(self):
        """删除权限"""
        return super(PermissionView, self).delete()
